package cn.zyy.hotel.service.impl;

import cn.zyy.hotel.dao.CarouselMapper;
import cn.zyy.hotel.entity.Carousel;
import cn.zyy.hotel.service.CarouselService;
import cn.zyy.hotel.utils.SystemConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
@Transactional
public class CarouselServiceImpl implements CarouselService {

    @Resource
    private CarouselMapper carouselMapper;

    //查询所有轮播图
    @Override
    public List<Carousel> selectCarouselList() {
        return carouselMapper.selectCarouselList();
    }

    //批量删除轮播图
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int batchDel(String ids) {
        //先查询要删除的轮播图，在本地删除
        List<Carousel> carousels = carouselMapper.selectCarouselListByIds(ids);
        //调用批量删除方法
        int line = carouselMapper.deleteByIds(ids);
        if (line > 0) {
            for (Carousel carousel : carousels) {
                new File(SystemConstant.IMAGE_UPLOAD_PATH,carousel.getUuidName()).delete();
            }
        }
        return line;
    }

    //删除轮播图
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteImg(Carousel carousel) {
        //调用删除方法
        int line = carouselMapper.deleteById(carousel.getId());
        if (line > 0){
            new File(SystemConstant.IMAGE_UPLOAD_PATH,carousel.getUuidName()).delete();
        }
        return line;
    }

    //添加轮播图
    @Override
    public int addCarousel(Carousel carousel) {
        return carouselMapper.addCarousel(carousel);
    }


}
